<template>
	<view class="content wrap">
		<u-form :model="form" ref="uForm"  :rules="rules">
			<u-form-item label="客户名称*" label-width="170" left-icon="account"><u-input v-model="form.goodsCustomerName" type="select" @click="getC()"/></u-form-item>
			
			<u-form-item label="商品名称*" label-width="170"><u-input v-model="form.goodsName"/></u-form-item>
			
			<u-form-item label="编号*" label-width="150"><u-input v-model="form.goodsNumber"/></u-form-item>
			
			<u-form-item label="货号" label-width="150"><u-input v-model="form.goodsNo"/></u-form-item>
			
			<u-form-item label="底*" label-width="150" prop="goodsBottom"><u-input v-model="form.goodsBottom" type="digit" placeholder="请输入底"/></u-form-item>
			
			<u-form-item label="叠层*" label-width="150"><u-input v-model="form.goodsLamination" type="digit" placeholder="请输入叠层"/></u-form-item>
			
			<u-form-item label="托层" label-width="150"><u-input v-model="form.goodsTraylayer" type="digit" placeholder="请输入托层"/></u-form-item>
			
			<u-form-item label="体积" label-width="150"><u-input v-model="form.goodsVolume" type="digit" placeholder="请输入体积"/></u-form-item>
			
			<u-form-item label="重量" label-width="150"><u-input v-model="form.goodsWeight" type="digit" placeholder="请输入重量"/></u-form-item>
			
			<u-form-item label="计费价" label-width="160" left-icon="rmb"><u-input v-model="form.goodsCharging" type="digit" placeholder="请输入计费价"/></u-form-item>
			
			<u-form-item label="保质期" label-width="160"><u-input v-model="form.goodsWarranty" type="digit" placeholder="请输入保质期月"/></u-form-item>
			
			<u-form-item label="箱规" label-width="150"><u-input v-model="form.xianggui" type="digit" placeholder="请输入箱规"/></u-form-item>
			
		</u-form>
		<u-button @click="submit">提交</u-button>
		<view>
			<u-select v-model="show" :list="clist" @confirm="confirm"></u-select>
		</view>
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				keyword:'',
				form:{
				    goodsCustomerId:'',
				    goodsCustomerName:'',
				    goodsName:'',
				    goodsNumber:'',
				    goodsNo:'',
				    goodsBottom:'',
				    goodsLamination:'',
				    goodsTraylayer:'',
				    goodsVolume:'',
				    goodsWeight:'',
				    goodsCharging:'',
				    goodsWarranty:'',
				    xianggui:'',
				},
				clist:[],
				show:false,
			    rules: {
					goodsBottom: [
						{ 
							required: true,
							message: '请输入数量', 
							trigger: 'blur',
						},
						{
							type:'number',
							message: '请输入正确的数据类型', 
							trigger: 'blur'
						}
					],
				}
			};
		},
		onLoad() {
			this.$http.post('/rest/customer/list',{
			},{
				params: {},
				header: {
					'Authorization' :uni.getStorageSync('token'),
				}
			}).then(res => {
				this.form.goodsCustomerId = res.data.data[0].id
				this.form.goodsCustomerName = res.data.data[0].customerName
				for(let item1 of res.data.data) {
					let obj = {}
					obj.value = item1.id
					obj.label = item1.customerName
					this.clist.push(obj)
				}
			})
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods:{
			getC(){
				this.show = true
			},
			confirm(e) {
				this.form.goodsCustomerId = e[0].value
				this.form.goodsCustomerName = e[0].label
			},
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						console.log('验证通过');
						if(typeof this.form.goodsCustomerId == 'undefined' || this.form.goodsCustomerId == null || this.form.goodsCustomerId == ""){
							uni.showToast({
								title: "请选择客户",
								icon: 'none'
							})
							return
						}
						if(typeof this.form.goodsName == 'undefined' || this.form.goodsName == null || this.form.goodsName == ""){
							uni.showToast({
								title: "请输入商品名称",
								icon: 'none'
							})
							return
						}
						if(typeof this.form.goodsNumber == 'undefined' || this.form.goodsNumber == null || this.form.goodsNumber == ""){
							uni.showToast({
								title: "请输入商品编号",
								icon: 'none'
							})
							return
						}
						if(typeof this.form.goodsBottom == 'undefined' || this.form.goodsBottom == null || this.form.goodsBottom == ""){
							uni.showToast({
								title: "请输入底",
								icon: 'none'
							})
							return
						}
						if(typeof this.form.goodsLamination == 'undefined' || this.form.goodsLamination == null || this.form.goodsLamination == ""){
							uni.showToast({
								title: "请输入叠层",
								icon: 'none'
							})
							return
						}
						uni.showLoading({
							title: '正在提交'
						})
						this.$http.post('/rest/storage/addItem',{
						},{
							params: this.form,
							header: {
								'Authorization' :uni.getStorageSync('token'),
							}
						}).then(res => {
							uni.hideLoading()
							if(res.data.code == 200) {
								this.$refs.uToast.show({
									title: '添加成功',
									type: 'success'
								})
								setTimeout(() => {
									this.$Router.back()
								}, 1000)
							} else {
								this.$refs.uToast.show({
									title: res.data.message,
									type: 'error'
								})
							}
						})
					} else {
						console.log('验证失败');
					}
				});
			},
		}
	}
</script>
<style lang="scss">
	page{
			background-color: #ffffff;
	}
	.wrap {
		padding: 30rpx;
	}
	.title{
		font-size: 28rpx;
		font-weight: 600;
		color: #2b85e4;
	}
</style>
